package mainProcess;
// class MainProcessing
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.lo23.network.*;
import ihm.main.Traitement;

import com.lo23.data.objgroup.*;
import com.lo23.data.objinterface.*;
import com.lo23.data.objpicture.*;
import com.lo23.data.test.*;
import com.lo23.data.utils.*;
import com.lo23.data.xml.*;

public class MainProcessing {
    //private int UID;
    //private String username;
	private UserAction userAction;
	private ProtocolListener ProtocolListener;
	private NetworkManager networkManager;
	private List<User> listOnlineUsers;
	private DataInterface dataInterface;
	private TraitementData traitementData;
	//private List<UserProfile> listFriendsProfile;

	// public methodes
	public MainProcessing(/*InterfaceIHM *IHM::Main::Traitement*/)
        {
		this.userAction = new UserAction();
		this.traitementData = new TraitementData();
		this.ProtocolListener = new ProtocolListener(this);
		this.listOnlineUsers = new ArrayList();
		//this.listFriendsProfile = new ArrayList();
                
        //TO DO: faire les setMainProcessing des classes qui en ont besoin
       userAction.setMainProcessing(this);
	}

        public String getUsername() {
            LocalUser lUser = this.dataInterface.getLocalUser(null);
            return lUser.getLogin();
        }
	
	public void setDataInterface(DataInterface dataInterface) {
		//TO DO: faire les setDataInterface des autres classes qui en ont besoin
		userAction.setDataInterface(dataInterface);
		traitementData.setDataInterface(dataInterface);
		this.dataInterface = dataInterface;
	}
	
	public DataInterface getDataInterface() {
		return this.dataInterface ;
	}
	
	public void setListOnlineUsers(List<User> users) {
		this.listOnlineUsers = users;
	}
	
	public List<User> getListOnlineUsers() {
		return this.listOnlineUsers;
	}
	
	public void addOnlineUser(User newUser) {
		this.listOnlineUsers.add(newUser);
                Traitement.getInstance().userConnection(newUser);
	}

        public User getUserWithUID(String uid) {
            for (User user: this.listOnlineUsers)
                if (user.getuID().equals(uid))
                    return user;
            return null;
        }
	
	public NetworkManager getNetworkManager()
	{
		return this.networkManager;
	}
	
	public void removeOnlineUser(User user) {
		int index = this.listOnlineUsers.indexOf(user);
		this.listOnlineUsers.remove(index);
                Traitement.getInstance().userDisconnection(user);
	}
	
	public TraitementData getTraitementData(){
		return this.traitementData;
	}
	
	public ProtocolListener getProtocolListener(){
		return this.ProtocolListener;
	}
	
	public void setNetworkManager(NetworkManager n){
		this.networkManager = n;
	}
	
	/*public UserProfile getFriendProfile(String friendID) {
		Iterator itr = this.listFriendsProfile.iterator();
		while (itr.hasNext()) {
			UserProfile p = itr.next();
			if (p.getUID() == friendID) {
				return p;
			}
		}
	}*/
	
	/*public void addFriendProfile(UserProfile p) {
		this.listFriendsProfile.add(p);
	}*/

}
